package com.ruoyi.account.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.ruoyi.account.enums.ApplyStatusEnum;
import com.ruoyi.db.domain.*;
import com.ruoyi.db.mapper.*;
import com.ruoyi.laiqianba.enums.AccountTypeEnum;
import com.ruoyi.account.enums.ActionTypeEnum;
import com.ruoyi.account.service.IAccountApplyRecordService;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.db.model.EntityParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 账户入账申请Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-28
 */
@Service
@DataSource(DataSourceType.SLAVE)
public class AccountApplyRecordServiceImpl implements IAccountApplyRecordService 
{
    @Resource
    private AccountApplyRecordMapper accountApplyRecordMapper;
    @Resource
    private ProductBaseInfoMapper productBaseInfoMapper;//获取产品
    @Resource
    private MerchantBaseInfoMapper merchantBaseInfoMapper;//获取商户机构
    @Resource
    private AccountInfoMapper accountInfoMapper;


    /**
     * 查询账户入账申请
     * 
     * @param id 账户入账申请主键
     * @return 账户入账申请
     */
    @Override
    public AccountApplyRecord selectAccountApplyRecordById(Long id)
    {
        AccountApplyRecord accountApplyRecord = accountApplyRecordMapper.selectAccountApplyRecordById(id);
        return accountApplyRecord;
    }

    /**
     * 查询账户入账申请列表
     * 
     * @param accountApplyRecord 账户入账申请
     * @return 账户入账申请
     */
    @Override
    public List<AccountApplyRecord> selectAccountApplyRecordList(AccountApplyRecord accountApplyRecord)
    {
        return accountApplyRecordMapper.selectAccountApplyRecordList(accountApplyRecord);
    }

    /**
     * 查询账户入账申请列表
     *
     * @param accountApplyRecord 账户入账申请
     * @return 账户入账申请
     */
    @Override
    public List<Map<String, Object>> selectAccountApplyRecordMapList(AccountApplyRecord accountApplyRecord)
    {
        return accountApplyRecordMapper.selectAccountApplyRecordMapList(accountApplyRecord);
    }

    /**
     * 新增账户入账申请
     * 
     * @param accountApplyRecord 账户入账申请
     * @return 结果
     */
    @Override
    public int insertAccountApplyRecord(AccountApplyRecord accountApplyRecord)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        accountApplyRecord.setCreatorId(loginUser.getUserId());
        accountApplyRecord.setCreatorName(loginUser.getUser().getNickName());

        Long itemAccountId = accountApplyRecord.getItemAccountId();//子账单id
        String itemAccountType = accountApplyRecord.getItemAccountType();//账户类型

        //通过账户类型获取到是哪个子账户下的，将其id和name拿到，返回前端
        AccountTypeEnum entityByType = AccountTypeEnum.getEntityByType(itemAccountType);
            switch(entityByType)
            {
                case PRODUCT://产品
                    ProductBaseInfo productBaseInfo = productBaseInfoMapper.selectProductBaseInfoById(itemAccountId);
                    accountApplyRecord.setItemAccountName(productBaseInfo.getProductName());
                    break;
                case MERCHANT://商户机构
                    MerchantBaseInfo merchantBaseInfo = merchantBaseInfoMapper.selectMerchantBaseInfoById(itemAccountId);
                    accountApplyRecord.setItemAccountName(merchantBaseInfo.getInfoName());
                    break;
//                case CHANNEL://渠道
//                    AccountChannelInfo accountChannelInfo = accountChannelInfoMapper.selectAccountChannelInfoById(itemAccountId);
//                    accountApplyRecord.setItemAccountName(accountChannelInfo.getName());
//                    break;
                case ACCOUNT://总账户
                    AccountInfo accountInfo = accountInfoMapper.selectAccountInfoById(itemAccountId);
                    accountApplyRecord.setItemAccountName(accountInfo.getName());
                    break;
                default:
                    AjaxResult.success();
            }
        //创建时间
        accountApplyRecord.setDateInfo(LocalDate.now().toString());
        accountApplyRecord.setCreateTime(DateUtils.getNowDate());

        if (ObjUtil.isNotEmpty(accountApplyRecord.getMainAccountId())){
            AccountInfo accountInfo = accountInfoMapper.selectAccountInfoById(accountApplyRecord.getMainAccountId());
            accountApplyRecord.setMainAccountName(accountInfo.getName());
        }

        return accountApplyRecordMapper.insertAccountApplyRecord(accountApplyRecord);
    }

    /**
     * 修改账户入账申请
     * 
     * @param accountApplyRecord 账户入账申请
     * @return 结果
     */
    @Override
    public int updateAccountApplyRecord(AccountApplyRecord accountApplyRecord)
    {
        if (ObjUtil.isNotEmpty(accountApplyRecord.getMainAccountId())){
            AccountInfo accountInfo = accountInfoMapper.selectAccountInfoById(accountApplyRecord.getMainAccountId());
            accountApplyRecord.setMainAccountName(accountInfo.getName());
        }

        return accountApplyRecordMapper.updateAccountApplyRecord(accountApplyRecord);
    }

    /**
     * 批量删除账户入账申请
     * 
     * @param ids 需要删除的账户入账申请主键
     * @return 结果
     */
    @Override
    public int deleteAccountApplyRecordByIds(Long[] ids)
    {
        return accountApplyRecordMapper.deleteAccountApplyRecordByIds(ids);
    }

    /**
     * 删除账户入账申请信息
     * 
     * @param id 账户入账申请主键
     * @return 结果
     */
    @Override
    public int deleteAccountApplyRecordById(Long id)
    {
        AccountApplyRecord accountApplyRecord = new AccountApplyRecord();
        accountApplyRecord.setId(id);
        accountApplyRecord.setStatus(ApplyStatusEnum.DELETE.getValue());
        return accountApplyRecordMapper.updateAccountApplyRecord(accountApplyRecord);
    }


}
